package net.softwarefabrik.bde.core.model.platform.internal;

import java.io.File;

import org.osgi.framework.Constants;

import net.softwarefabrik.bde.core.model.BDEConstants;
import net.softwarefabrik.bde.core.model.BDEException;
import net.softwarefabrik.bde.core.model.maven.RepositoryManager;
import net.softwarefabrik.bde.core.model.platform.BundleInfo;
import net.softwarefabrik.bde.core.model.platform.PlatformConfiguration;
import net.softwarefabrik.bde.core.model.platform.internal.AbstractPlatformProvider;
import net.softwarefabrik.bde.core.util.FileUtil;

/**
 * A <code>PlatformProvider</code> for the Equinox OSGi platform.
 * 
 * @see http://www.eclipse.org/equinox/
 * @author Matthias Kuespert
 */
public class EquinoxPlatformProvider extends AbstractPlatformProvider {

    public EquinoxPlatformProvider(RepositoryManager repositoryManager) {
        super(repositoryManager);
    }
    
    /**
     * @see net.softwarefabrik.bde.core.model.platform.internal.AbstractPlatformProvider#getArtifactId()
     */
    public String getArtifactId() {
        return "org.eclipse.osgi";
    }
    
    /**
     * @see net.softwarefabrik.bde.core.model.platform.internal.AbstractPlatformProvider#getGroupId()
     */
    public String getGroupId() {
        return "org.eclipse.osgi";
    }
    
    /**
     * @see net.softwarefabrik.bde.core.model.platform.PlatformProvider#getPlatformName()
     */
    public String getPlatformName() {
        return "Equinox";
    }

    /**
     * @see net.softwarefabrik.bde.core.model.platform.PlatformProvider#getMainClass(net.softwarefabrik.bde.core.model.platform.PlatformConfiguration)
     */
    public String getMainClass() {
        return "org.eclipse.core.runtime.adaptor.EclipseStarter";
    }

    /**
     * @see net.softwarefabrik.bde.core.model.platform.internal.AbstractPlatformProvider#getCacheDir()
     */
    @Override
    protected String getCacheDir() {
        return getPlatformId() + BDEConstants.STD_FILE_SEPARATOR + "configuration";
    }
    
    /**
     * @see net.softwarefabrik.bde.core.model.platform.internal.AbstractPlatformProvider#getConfigFile()
     */
    @Override
    protected String getConfigFile() {
        return getCacheDir() + BDEConstants.STD_FILE_SEPARATOR + "config.ini";
    }
    
    /**
     * @see net.softwarefabrik.bde.core.model.platform.PlatformProvider#getRuntimeArguments(net.softwarefabrik.bde.core.model.platform.PlatformConfiguration)
     */
    public String[] getRuntimeArguments(PlatformConfiguration config) throws BDEException {
        return new String[] {
                             "-configuration "
                                 + config.getWorkingDirectory()
                                 + BDEConstants.STD_FILE_SEPARATOR
                                 + getPlatformId(),
                             "-console" 
                            };
    }

    /**
     * @see net.softwarefabrik.bde.core.model.platform.PlatformProvider#getRuntimeVMOptions(net.softwarefabrik.bde.core.model.platform.PlatformConfiguration)
     */
    public String[] getRuntimeVMOptions(PlatformConfiguration config) throws BDEException {
        if (null == config) {
            throw new BDEException("No PlatformConfiguration object specified.");
        }
        String workingDirectory = config.getWorkingDirectory(); 
        if (null == workingDirectory || BDEConstants.EMPTY_STRING.equals(workingDirectory)) {
            throw new BDEException("No working directory specified for platform " + getPlatformName());
        }
        //
        return new String[] {
                             "-Dosgi.install.area="
                              + workingDirectory
                              + BDEConstants.STD_FILE_SEPARATOR
                              + getPlatformId(),
                            };
    }

    /**
     * @see net.softwarefabrik.bde.core.model.platform.internal.AbstractPlatformProvider#createRuntimeConfiguration(net.softwarefabrik.bde.core.model.platform.PlatformConfiguration)
     */
    protected void createRuntimeConfiguration(PlatformConfiguration config) throws BDEException {
        StringBuffer content = new StringBuffer();
        content.append("# ----------------------------------------------------------------------------\n");
        content.append("# Equinox configuration created by bde.platform.equinox plugin\n");
        content.append("# THIS IS A GENERATED FILE - DO NOT EDIT!\n");
        content.append("# ----------------------------------------------------------------------------\n");
        content.append("# Framework settings:\n");
        content.append("#\n");
        content.append("osgi.startLevel=" + config.getPlatformStartLevel() + "\n");
        content.append("osgi.bundles.defaultStartLevel=" + config.getDefaultBundleStartLevel() + "\n");
        content.append("osgi.clean=true\n"); // always clear cache
        content.append(  Constants.FRAMEWORK_SYSTEMPACKAGES + "="
                       + "org.osgi.framework; version=1.3.0, \\\n"
                       + "org.osgi.service.packageadmin; version=1.2.0, \\\n"
                       + "org.osgi.service.startlevel; version=1.0.0, \\\n"
                       + "org.osgi.service.url; version=1.0.0, \\\n"
                       + "org.osgi.util.tracker; version=1.3.2\n");
                       // + "${jre-${java.specification.version}}\n");
        
        content.append("#\n");
        content.append("# ----------------------------------------------------------------------------\n");
        content.append("# Bundle configuration\n");
        content.append("#\n");
        content.append("osgi.bundles=");
        boolean first = true;
        for (BundleInfo bundle : config.getBundles()) {
            if (!first) {
                content.append(",\\\n ");
            }
            first = false;
            content.append("file:" + bundle.getPath()
                           + "@"
                           + bundle.getStartLevel()
                           + (bundle.doStart() ? ":start" : BDEConstants.EMPTY_STRING));
        }
        content.append(BDEConstants.STD_LINE_SEPARATOR);
        //
        File cacheDir = new File(config.getWorkingDirectory()
                                 + BDEConstants.STD_FILE_SEPARATOR
                                 + getCacheDir());
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }
        String configFileName = config.getWorkingDirectory()
                                + BDEConstants.STD_FILE_SEPARATOR
                                + getConfigFile();
        FileUtil.writeFile(configFileName, content.toString());
    }
}
